<%@ page language="java" pageEncoding="UTF-8"   isELIgnored="false"%>


<%@page import="util.ServletHelp"%>
<%@page import="global.Constants;"%>


<html>
<head>
	<%
		String baseUrl = request.getContextPath();
	%>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script type="text/javascript">
	   //根据主表id查找从表
	   var baseParams = {limit:<%=Constants.PAGE_SIZE %>,start : 0};
	   var baseParamsCong = {limit:<%=Constants.PAGE_SIZE %>,start : 0};
	   var EXT_QUERY_TIME_OUT = 1200000;
	   var dListStore = null ;
	   var pidd = '';   //  父id的值
	   var pname = '';   // 被选中的父类名称
      var paraShu = 0;
	
	
	
		$(document).ready(function(){
			// 全局参数
	

			// 用户数据源
		    var mListStore = new Ext.data.JsonStore({
		        url: '${pageContext.request.contextPath}/dictionary_findDictionary.action',
		        root: 'datas',
				timeout : EXT_QUERY_TIME_OUT,
		        totalProperty: 'results',
		        fields:  ['id', 'name', 'pid'],
		        baseParams: baseParams,
		        autoLoad: true,
				listeners : {  
          load : function() {  
              isDetailReady = true;// 查询置换标志为已查过   
            }  
        
        }  

		    });
		    
		       dListStore = new Ext.data.JsonStore({
		         url: '${pageContext.request.contextPath}/dictionary_findDictionary.action',
		        root: 'datas',
				timeout : EXT_QUERY_TIME_OUT,
		        totalProperty: 'results',
		        fields:  ['id', 'name', 'pid'],
		        baseParams: baseParamsCong,
		        autoLoad: false,
				listeners : {  
                load : function() {  
                isDetailReady = true;// 查询置换标志为已查过   
                }  
               }  
               })
		    
		             
           
            
			
		   	    // 工具栏
		    var mListToolbar = new Ext.Toolbar({
		    	items: [
					new Ext.Button({
					    id: 'dictionaryList-add-button',
					    text: '添加',
						iconCls: 'table_add'
						//disabled:true
					}),
					new Ext.Button({
					    id: 'dictionaryList-start-button',
					    text: '修改',
						iconCls: 'table_edit'
					}),
					new Ext.Button({
					    id: 'dictionaryList-delete-button',
					    text: '删除',
						iconCls: 'table_delete'
					}),
					new Ext.ux.form.SearchField({
		                store: mListStore,
		                width: 180,
		                paramName: 'name',
		                emptyText: '请输入字典名称...',
		                style: 'margin-left: 10px;'
		            })

				],
				width:350
			});

			// 用户数据表格
			var sm = new Ext.grid.CheckboxSelectionModel();
			var mListGrid = new Ext.grid.GridPanel({
				id: 'mListGrid',
		 		border: false,
				stateful: true,
			    autoScroll: 'auto',
		        store: mListStore,
		        loadMask: true,
		        bodyBorder:true,
		        cm: new Ext.grid.ColumnModel({
		            defaults: {
		                width: 100,
		                sortable: true
		            },
			        columns: [
						sm,
						new Ext.grid.RowNumberer({header:'№'}),
						{id:'id',header: '字典父类名称', width: 350, sortable: true, dataIndex: 'id',hidden:true},
			            {name:'name',header: '字典父类名称', width: 350, sortable: true, dataIndex: 'name'},
			            {pid:'pid',header: '父id', width: 0, sortable: true, dataIndex: 'pid'}
			            
			          
					]
		        }),
		        sm: sm,
		        columnLines: true,
		        height:220,
		        width:550,
		        bbar: new Ext.PagingToolbar({
					pageSize: <%=Constants.PAGE_SIZE %>,
					store: mListStore,
					displayInfo: true,
					displayMsg: Anynote.PAGINGTOOLBAR_DISPLAYMSG,
					emptyMsg: Anynote.PAGINGTOOLBAR_EMPTYMSG,
					doLoad: function(start){
						baseParams.start = start;
						this.store.load({params: baseParams});
					}
		        }),
		        listeners:{'rowclick':function(grid,rowIndex,event){     
		          
                 var selectionModel = grid.getSelectionModel();    
                var record = selectionModel.getSelected();
                   pidd = record.get("id");
                //var record = sm.getSelection()[0];
                  dListStore.load({
                    params : {
                        'para' : record.get("id")
                    },
                    callback: function(records, operation, success) {
                        if(success){
                            //record.entryList().loadRecords(records,{});
                            //createWin(record);
                        }
                    }
                });

                    }}
		         
		    });
			// 设置Grid高度和宽度
		//    	Anynote.resizeGridTo("userListGrid", 0, 56);
	//	var onrowdoubleclick = function(grid, index, e){
     //       alert(grid.get("id"));
     //   }
        
        
		
		//////////////
		
				   	    // 工具栏
		    var dListToolbar = new Ext.Toolbar({
		    	items: [
					new Ext.Button({
					    id: 'dictionaryList-add-button-d',
					    text: '添加',
						iconCls: 'table_add'
						//disabled:true
					}),
					new Ext.Button({
					    id: 'dictionaryList-start-button-d',
					    text: '修改',
						iconCls: 'table_edit'
					}),
					new Ext.Button({
					    id: 'dictionaryList-delete-button-d',
					    text: '删除',
						iconCls: 'table_delete'
					})
		           
				],
				bodyStyle: 'border-left-width:1px;',
			   width:350
			});

			// 用户数据表格
			var sm = new Ext.grid.CheckboxSelectionModel();
			var dListGrid = new Ext.grid.GridPanel({
				id: 'dListGrid',
				 border: false,
				stateful: true,
			    autoScroll: 'auto',
		        store: dListStore,
		        loadMask: true,
		         
		        cm: new Ext.grid.ColumnModel({
		            defaults: {
		                width: 100,
		                sortable: true
		            },
			        columns: [
						sm,
						new Ext.grid.RowNumberer({header:'№'}),
						{id:'id',header: '字典父类名称', width: 350, sortable: true, dataIndex: 'id',hidden:true},
			            {name:'name',header: '字典子类名称', width: 350, sortable: true, dataIndex: 'name'},
			            {pid:'pid',header: '父id', width:  0, sortable: true, dataIndex: 'pid'}
			            
					]
		        }),
		        sm: sm,
		        columnLines: true,
		        height:220,
		        width:550
		    });
		    

			var mPanel = new Ext.Panel({
				id: 'mPanel',
				region: 'west',
				border: false,
				bodyStyle: 'border-right-width:1px;',
				split: true,
                width: 500,
                minSize: 200,
                maxSize: 500,
				layout:'fit',
				tbar:mListToolbar,
                collapseMode:'mini',
				items: [mListGrid]

			});

               var dPanel = new Ext.Panel({
				id: 'dPanel',
				region: 'center',
				border: false,
				autoScroll : true,
				bodyStyle: 'border-left-width:1px;',
                width: 400,
                minSize: 200,
                maxSize: 500,
				layout:'fit',
				tbar:dListToolbar,
                collapseMode:'mini',
				items: [dListGrid]
				
			});


			// 主面板
			var noteListMainPanel = new Ext.Panel({
				id: 'noteListMainPanel',
				renderTo: 'noteListMainPanelDiv',
				border: false,
				layout: 'border',
				items: [mPanel, dPanel]
			});
			Anynote.resizeGridTo("noteListMainPanel", 0, 28);

			// 添加 字典主表 按钮
			$("#dictionaryList-add-button").click(function(){
		     
				
				// 新增用户Form
				var addDictionaryFormPanel = new Ext.FormPanel({
					id: 'addDictionaryFormPanel',
			        labelWidth: 60,
			        buttonAlign: 'center',
			        border: false,
			        bodyStyle: 'padding:10px;text-align:left;background-color:transparent;',
			        url: '',
		            items:[{// 名称
			                xtype:'textfield',
			                name: 'name',
			                fieldLabel: '属性名称',
			                anchor:'98%',
			                allowBlank:false,
			                maxLength: 20,
			                listeners:{
			                blur:function(n){
			                var va = n.getValue();
			                Ext.Ajax.request({
	                        url:  '${pageContext.request.contextPath}/proving_provingOnly.action',
		                     params:{val:va,filed:'name',table:'dictionary'},
		                    success: function(res){
		                      var isPorv = res.responseText;
		                      if(isPorv == 1){
		                        n.setValue('');
		                        Ext.Msg.alert('错误', '不能输入一样的名称');
		                        }
			                }
			                })
			                }
			                }
			           }, {   // 父id
			                xtype:'hidden',
			                name: 'pid',
			                fieldLabel: '父id',
			                anchor:'98%',
			                allowBlank:false,
			                maxLength: 20
			           }],
			        buttons: [{
	                    text:'提交',
	                    handler: function(){
	                       setTimeout(function(){
				        	var form = addDictionaryFormPanel.getForm();
						    if(form.isValid()){
						    	// 发送请求
								Anynote.ajaxRequest({
								 
									baseParams: form.getValues(),
									action: '${pageContext.request.contextPath}/dictionary_addDictionary.action',
									callback: function(jsonResult){
										mListGrid.getStore().reload();
										addDictionaryWindow.close();
										//   Ext.Msg.alert("成功");
									},
									showWaiting: true,
									failureMsg: '保存失败.'
								});
							}
							},9000)
	                	}
	                },{
	                    text: '重置',
	                    handler: function(){
	                		addDictionaryFormPanel.getForm().reset();
	                    }
	                }]
			    });
				// 增加字典窗口
				var addDictionaryWindow = new Ext.Window({
					title: '添加字典',
					width: 330,
					height: 120,
					iconCls: 'table_add',
					modal: true,
					maximizable: false,
					resizable: false,
					layout:'fit',
					plain: true,
					items: [addDictionaryFormPanel]
				}).show();
			});
			

			
			
			
			
			
									// 添加 字典从表 按钮
			$("#dictionaryList-add-button-d").click(function(){
			/////////////////////////
		     var selectionModel = mListGrid.getSelectionModel(); 
		        
                var record = selectionModel.getSelected();
           
                   if(record ==  undefined ){
                   Ext.Msg.alert("提示", "请先选择字典父类");
                       return  false;
                   }
                     
                 var   pname = record.get("name");   // 父类属性名称
                    
				////////////////////////////////////
				// 新增 Form
				var addDictionaryFormPanel = new Ext.FormPanel({
					id: 'addDictionaryFormPanel',
			        labelWidth: 60,
			        buttonAlign: 'center',
			        border: false,
			        bodyStyle: 'padding:10px;text-align:left;background-color:transparent;',
			        url: '',
		            items:[{// 名称
			                xtype:'textfield',
			                name: 'pname',
			                fieldLabel: '所属父类',
			                value: pname,
							readOnly:true,
			                anchor:'98%',
			                allowBlank:false,
			                maxLength: 20
			           },{// 名称
			                xtype:'textfield',
			                name: 'name',
			                fieldLabel: '字典名称',
			                anchor:'98%',
			                allowBlank:false,
			                maxLength: 20,
			                listeners:{
			                blur:function(n){
			                
			                var va = n.getValue();
			                Ext.Ajax.request({
			                 
	                        url:  '${pageContext.request.contextPath}/proving_provingOnly.action',
		                     params:{val:va,filed:'name',table:'dictionary'},
		                    success: function(res){
		                      var isPorv = res.responseText;
		                      if(isPorv == 1){
		                        n.setValue('');
		                        Ext.Msg.alert('错误', '不能输入一样的名称');
		                        }
			                }
			                })
			                }
			                }
			           }, {   // 父id
			                xtype:'hidden',
			                name: 'pid',
			                fieldLabel: '父id',
			                anchor:'98%',
			                allowBlank:false,
			                value: pidd,
			                maxLength: 20
			           }],
			        buttons: [{
	                    text:'提交',
	                    handler: function(){
	                    setTimeout(function(){
				        	var form = addDictionaryFormPanel.getForm();
				        
						    if(form.isValid()){
						    	// 发送请求
						    	
								Anynote.ajaxRequest({
								 
									baseParams: form.getValues(),
									action: '${pageContext.request.contextPath}/dictionary_addDictionary.action',
									callback: function(jsonResult){
										dListGrid.getStore().reload();
										addDictionaryWindowd.close();
								 
									},
									showWaiting: true,
									failureMsg: '保存失败.'
								});
							}
							},9000)
	                	}
	                },{
	                    text: '重置',
	                    handler: function(){
	                		addDictionaryFormPanel.getForm().reset();
	                    }
	                }]
			    });
				// 增加字典窗口
				var addDictionaryWindowd = new Ext.Window({
					title: '添加字典',
					width: 330,
					height: 160,
					iconCls: 'table_add',
					modal: true,
					maximizable: false,
					resizable: false,
					layout:'fit',
					plain: true,
					items: [addDictionaryFormPanel]
				}).show();
			});
			
			
		
			
			///////////////////   修改主表
			
			$("#dictionaryList-start-button").click(function(){
					// 修改Form
			    var selectionModel = mListGrid.getSelectionModel(); 
		        var record = selectionModel.getSelected();
                   if(record ==  undefined ){
                   Ext.Msg.alert("提示", "请先一条字典数据");
                       return  false;
                   }
                   
                   updateDictionray(record,mListGrid);
              })
                   //修改从表
             $("#dictionaryList-start-button-d").click(function(){
					// 修改Form
			    var selectionModel = dListGrid.getSelectionModel(); 
		        var record = selectionModel.getSelected();
                   if(record ==  undefined ){
                   Ext.Msg.alert("提示", "请先一条字典数据");
                       return  false;
                   }
                   
                   updateDictionray(record,dListGrid);
              })
                  
                  
                  // 删除主表按钮
			$("#dictionaryList-delete-button").click(function(){
				// 提交
				 deleteDictionray(mListGrid,dListGrid,0);
			});
			
			      // 删除从表按钮
			$("#dictionaryList-delete-button-d").click(function(){
				// 提交
				 deleteDictionray(dListGrid,null,1);
			});
			 
			
			
			///////////////////
			//  修改 字典
			   function updateDictionray(record,listGrid){
			   var   id = record.get("id");   // 父类属性id
                     var   name = record.get("name");   // 父类属性名称
                     
			 var updateDictionaryFormPanel = new Ext.FormPanel({
					id: 'updateDictionaryFormPanel',
			        labelWidth: 60,
			        buttonAlign: 'center',
			        border: false,
			        bodyStyle: 'padding:10px;text-align:left;background-color:transparent;',
			        url: '',
		            items:[{// id
			                xtype:'hidden',
			                name: 'id',
			                fieldLabel: '属性编号',
			                anchor:'98%',
			                allowBlank:false,
			                value:id,
			                maxLength: 50
			           },{// 名称
			                xtype:'textfield',
			                name: 'name',
			                fieldLabel: '属性名称',
			                anchor:'98%',
			                allowBlank:false,
			                value:name,
			                maxLength: 50,
			                listeners:{
			                blur:function(n){
			                
			                var va = n.getValue();
			                Ext.Ajax.request({
			                 
	                        url:  '${pageContext.request.contextPath}/proving_provingOnly.action',
		                     params:{val:va,filed:'name',table:'dictionary'},
		                    success: function(res){
		                      var isPorv = res.responseText;
		                      if(isPorv == 1){
		                        n.setValue('');
		                        Ext.Msg.alert('错误', '不能输入一样的名称');
		                        }
			                }
			                })
			                }
			                }
			           }],
			        buttons: [{
	                    text:'提交',
	                    handler: function(){
	                        
	                    
	                       setTimeout(function(){
				        	var form = updateDictionaryFormPanel.getForm();
						    if(form.isValid()){
						    	// 发送请求
								Anynote.ajaxRequest({
								 
									baseParams: form.getValues(),
									action: '${pageContext.request.contextPath}/dictionary_updateDictionary.action',
									callback: function(jsonResult){
										listGrid.getStore().reload();
										updateDictionaryWindow.close();
										//   Ext.Msg.alert("成功");
									},
									showWaiting: true,
									failureMsg: '保存失败.'
								});
							}
							},9000)
	                	}
	                },{
	                    text: '重置',
	                    handler: function(){
	                		updateDictionaryFormPanel.getForm().reset();
	                    }
	                }]
			    });
				// 增加字典窗口
				var updateDictionaryWindow = new Ext.Window({
					title: '修改字典属性',
					width: 330,
					height: 120,
					modal: true,
					iconCls: 'table_edit',
					maximizable: false,
					resizable: false,
					layout:'fit',
					plain: true,
					items: [updateDictionaryFormPanel]
				}).show();		
                  }
                  
                  
                  //删除
			function deleteDictionray(listGrid,dlistGrid,layer){
			    var records = listGrid.getSelectionModel().getSelections();
		        
		   //     alert(records.length+'  ------');
			   if(records.length < 1){
					 Ext.Msg.alert("提示", "请至少选择一条数据.");
				}else{
					   //如果点击的是删除用户
					    //  var  id = record.get("id");   // 父类属性id
					      Ext.Msg.confirm("警告", '是否删除数据', function(btn){
						  if(btn=="yes"){
							var dictIds = "";
							for(var i=0;i<records.length;i++){
								dictIds = dictIds + records[i].get("id") + ",";
							}
							// 发送请求
							Anynote.ajaxRequest({
								 
								baseParams: {id:dictIds,layer:layer},
								action: '${pageContext.request.contextPath}/dictionary_deleteDictionary.action',
								callback: function(jsonResult){
									listGrid.getStore().reload();
									  if(dlistGrid != null){
									    
									  dlistGrid.getStore().reload();
									}
								},
								showWaiting: true,
								failureMsg: '保存失败.'
							});
					 
					     }
						 });
				  }
			}
		
});

      
       
       
       
          
	</script>
</head>
<body>
<div id="noteListMainPanelDiv"></div>

</body>
</html>
